package com.mt.customer.service.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.customer.constants.CustomerConstant;
import com.mt.customer.entity.CustomerAccountEntity;
import com.mt.customer.entity.CustomerEntity;
import com.mt.customer.mapper.CustomerAccountEntityMapper;
import com.mt.customer.mapper.CustomerEntityMapper;

@Component
public class CustomerServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CustomerServiceModel.class);

    @Autowired
    private CustomerEntityMapper customerEntityMapper;

    @Autowired
    private CustomerAccountEntityMapper customerAccountEntityMapper;

    public ServiceResult<List<CustomerEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.customerEntityMapper, "Property 'customerEntityMapper' is required.");
        ServiceResult<List<CustomerEntity>> result = new ServiceResult<List<CustomerEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.customerEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<CustomerEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.customerEntityMapper, "Property 'customerEntityMapper' is required.");
        ServiceResult<CustomerEntity> result = new ServiceResult<CustomerEntity>();
        try {
            result.setResult(this.customerEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> insert(CustomerEntity customerEntity) {
        Assert.notNull(this.customerEntityMapper, "Property 'customerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        Date now = new Date();
        try {
            int save = this.customerEntityMapper.insert(customerEntity);
            if (save > 0) {
                id = customerEntity.getCustomerId();
                // 创建用户成功后，创建用户账户数据
                CustomerAccountEntity customerAccountEntity = new CustomerAccountEntity();
                customerAccountEntity.setCustomerId(id);
                customerAccountEntity.setStatus(CustomerConstant.CUSTOMER_ACCOUNT_STATUS_1);
                customerAccountEntity.setBalance(new BigDecimal(0.00));
                customerAccountEntity.setBalanceWithdrawal(new BigDecimal(0.00));
                customerAccountEntity.setBalanceNonWithdrawal(new BigDecimal(0.00));
                customerAccountEntity.setCreateTime(now);
                customerAccountEntity.setUpdateTime(now);
                customerAccountEntityMapper.insert(customerAccountEntity);
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerServiceModel][insert]：query insert occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(CustomerEntity customerEntity) {
        Assert.notNull(this.customerEntityMapper, "Property 'customerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.customerEntityMapper.updateByPrimaryKeySelective(customerEntity);
            if (save > 0) {
                id = customerEntity.getCustomerId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(CustomerEntity customerEntity) {
        Assert.notNull(this.customerEntityMapper, "Property 'customerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.customerEntityMapper.updateByPrimaryKeySelective(customerEntity);
            if (save > 0) {
                id = customerEntity.getCustomerId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer customerId) {
        Assert.notNull(this.customerEntityMapper, "Property 'customerEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.customerEntityMapper.deleteByPrimaryKey(customerId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
